package com.ophiux.cs.infrastructure.repositories.consultOrder;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ophiux.cs.domain.consultOrder.model.aggregate.OnlineRecordsAndDetail;
import com.ophiux.cs.domain.consultOrder.model.entity.CSOnlineRecords;
import com.ophiux.cs.domain.consultOrder.model.entity.CSRecordDescription;
import com.ophiux.cs.domain.consultOrder.model.valueobj.OnlineRecordValueObj;
import com.ophiux.cs.domain.consultOrder.repositories.ICSOnlineRecordRepository;
import com.ophiux.cs.infrastructure.mapper.consultOrder.ConsultOrderDescriptionMapper;
import com.ophiux.cs.infrastructure.mapper.consultOrder.ConsultOrderMapper;
import com.ophiux.utils.exception.OphiuxRepositoryException;

import lombok.extern.slf4j.Slf4j;

@Repository
@Slf4j
public class CSOnlineRecordRepository implements ICSOnlineRecordRepository {
	@Autowired
	ConsultOrderMapper consultOrderMapper;
	@Autowired
	ConsultOrderDescriptionMapper consultOrderDescriptionMapper;
	
	@Override
	@Transactional
	public void save(CSOnlineRecords obj,CSRecordDescription obj1) throws OphiuxRepositoryException {
		try {
			CSOnlineRecords newObj = consultOrderMapper.getRecordById(obj.getRECORD_ID());
			
			if(newObj==null) {//数据库没有,则执行新增操作
				consultOrderMapper.insert(obj);
				
				consultOrderDescriptionMapper.insert(obj1);
			}else {
				consultOrderMapper.update(obj);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息",e);
			throw new OphiuxRepositoryException(e);
		}
	}

	@Override
	public void update(CSOnlineRecords obj) throws OphiuxRepositoryException {
		try {
			consultOrderMapper.update(obj);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息",e);
			throw new OphiuxRepositoryException(e);
		}
	}

	@Override
	public List<OnlineRecordsAndDetail> getInfoList(OnlineRecordValueObj obj) throws OphiuxRepositoryException {
		List<OnlineRecordsAndDetail> list = new ArrayList<OnlineRecordsAndDetail>();
		try {
			list = consultOrderMapper.getInfoList(obj);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息",e);
			throw new OphiuxRepositoryException(e);
		}
		return list;
	}

	@Override
	public CSOnlineRecords getRecordById(String recordId) throws OphiuxRepositoryException{
		CSOnlineRecords record = new CSOnlineRecords();
		try {
			record = consultOrderMapper.getRecordById(recordId);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息",e);
			throw new OphiuxRepositoryException(e);
		}
		return record;
	}

}
